package main

import (
	"encoding/json"
	"flag"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
)

func test2_main() {
	MyPrintf("\n ------test2 ----top-\n")
	testPremValue()
	MyPrintf("\n ------test2 ----end-\n")
}

func test_getpostdata() {
	db := httpPost("http://www.robobloq.com:8481/p", "c=sv.hexback&o=mega&d=766f69642073657475702829207b0a0a7d0a0a766f6964206c6f6f702829207b0a0a7d")
	MyPrintf(db)
}

func testJson2Obj() {
	ss := `{"c":"version","d":"180516.0"}`
	var j ClientJSONData
	err := json.Unmarshal([]byte(ss), &j)
	if err != nil {
		MyPrintf("Problem decoding json. giving up. json:%v, err:%v\n", ss, err)
		return
	}
	MyPrintf(" json c= %s", j.C)
}

func testHexData(w http.ResponseWriter, r *http.Request) {
	//var todo Todo
	body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
	if err != nil {
		//MyPrintf(err)
		return
	}
	if err := r.Body.Close(); err != nil {
		//MyPrintf(err)
		return
	}
	log.Printf(string(body)) //{ name: "John", time: "2pm" }

	var dat map[string]interface{}
	json.Unmarshal(body, &dat) //[]byte(json_str)
	//http://blog.csdn.net/qq_33204116/article/details/50700705
	// get string
	if v2, ok2 := dat["c"]; ok2 {
		name := " AA" + v2.(string)
		log.Printf(name)
	}
	// get number
	if v3, ok3 := dat["d"]; ok3 {
		//p3 := strconv.FormatInt( v3.(int64) ,10)
		p3 := strconv.FormatFloat(v3.(float64), 'g', 1, 64)
		log.Printf(p3)
	}
	u := Message{P: "liamPost", D: "123", O: "time.Now()", I: "asd1234"}
	if err := json.NewEncoder(w).Encode(u); err != nil {
		panic(err)
	}
	return
}

var testWhttp http.ResponseWriter

func testJsonForm(w http.ResponseWriter, r *http.Request) {
	testWhttp = w
	u := Message{P: "liamPostA", D: "123", O: "time.Now()", I: "asd1234"}
	// if err := json.NewEncoder(w).Encode(u); err != nil {
	// 	panic(err)
	// }
	testJsonFormSend(u)
}

func testJsonFormSend(db Message) {
	u := Message{P: "liamPostB", D: "123", O: "time.Now()", I: "asd1234"}
	if err := json.NewEncoder(testWhttp).Encode(u); err != nil {
		panic(err)
	}
}

// ./robobloqSerial -sport=8805 -hport=8806 -ide=/Applications/Arduino.app/Contents/MacOS/Arduino
// https://blog.csdn.net/guanchunsheng/article/details/79612153
func testPremValue() {
	var sport = flag.String("sport", "", "sport")
	var hport = flag.String("hport", "", "hport")
	var ide = flag.String("ide", "", "ide")
	flag.Parse()
	MyPrintf("\n sport= %v \n", *sport)
	MyPrintf("\n hport= %v \n", *hport)
	MyPrintf("\n ide= %v \n", *ide)
}
